package com.xueyi.game.backend.entity.dto;

import com.xueyi.core.base.annotation.Add;
import com.xueyi.core.constants.SystemCode;
import com.xueyi.core.expection.BusinessException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Update;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author: hxs
 * @createDate: 2022/11/9 17:37
 * @description: 新增、修改权限
 */
@Data
public class AddOrUpdateJurisdictionDto {

    /**
     * id
     */
    @NotNull(message = "参数异常", groups = Update.class)
    private Integer id;

    /**
     * 名称
     */
    @NotBlank(message = "权限名称不能为空", groups = {Add.class, Update.class})
    @Length(min = 1, max = 50, message = "名称最大长度为50", groups = {Add.class, Update.class})
    private String menuName;

    /**
     * pid
     */
    @NotNull(message = "请选择所属父级", groups = {Add.class, Update.class})
    private Integer parentId;

    /**
     * 图标
     */
    private String menuIcon;

    /**
     * 路径
     */
    @NotBlank(message = "路径不能为空", groups = {Add.class, Update.class})
    @Length(min = 1, max = 500, message = "路径最大长度为500", groups = {Add.class, Update.class})
    private String menuPath;

    /**
     * 类型
     */
    @NotNull(message = "类型不能为空", groups = {Add.class, Update.class})
    private Integer authorityType;

    /**
     * 排序值
     */
    @NotNull(message = "排序值不能为空",groups = {Add.class,Update.class})
    private Integer ordered;

    public void format() throws BusinessException {
        if (authorityType.equals(1) && StringUtils.isBlank(menuIcon) && parentId.equals(0)) {
            throw new BusinessException(SystemCode.QUERY_CHECK_ERROR, "菜单图标不能为空");
        } else {
            menuIcon = StringUtils.isBlank(menuIcon) ? null : menuIcon.trim();
        }

        menuName = StringUtils.isBlank(menuName) ? null : menuName.trim();

        menuPath = StringUtils.isBlank(menuPath) ? null : menuPath.trim();

    }

}
